package qc.module.qms.service.alarm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import qc.common.core.constants.QmsCacheConstant;
import qc.module.qms.entity.QmsAlarmDataPoint;
import qc.module.qms.entity.QmsAlarmDataRule;
import qc.module.qms.repository.QmsAlarmDataPointRepository;
import qc.module.qms.repository.QmsAlarmDataRuleRepository;

import java.util.List;

/**
 * 告警规则Service，提供告警规则的维护和查询
 *
 * @author QuCheng Tech
 * @since 2025/10/20
 */
@Service
public class QmsAlarmDataRuleService {
    private static final Logger logger = LoggerFactory.getLogger(QmsAlarmDataRuleService.class);

    private static final String Cache_Key_Rule = QmsCacheConstant.CACHE_KEY_ALARM_DATA_RULE_ENTITY;

    private static final String Cache_Key_Rule_Point = QmsCacheConstant.CACHE_KEY_ALARM_DATA_RULE_POINT_RELATION_ENTITY;

    @Autowired
    private QmsAlarmDataRuleRepository ruleRepository;

    @Autowired
    private QmsAlarmDataPointRepository pointRepository;

    /**
     * 更新缓存，不管是否有缓存数据都进行更新
     *
     * @return java.util.List<qc.module.qms.entity.QmsAlarmDataRule>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @CachePut(value = Cache_Key_Rule)//更新缓存，不管是否有缓存数据都进行更新
    public List<QmsAlarmDataRule> ruleCachePut() {
        logger.debug("ruleCachePut");

        return getAllRulesFromDb();
    }

    /**
     * 清除缓存
     *
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @CacheEvict(value = Cache_Key_Rule)//清除缓存
    public void ruleCacheEvict() {
        logger.debug("ruleCacheEvict");
    }

    /**
     * 获取全部；有缓存时从缓存获取，没有缓存时从数据库中获取并放入缓存
     *
     * @return java.util.List<qc.module.qms.entity.QmsAlarmDataRule>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @Cacheable(value = Cache_Key_Rule)
    public List<QmsAlarmDataRule> getAllRulesWithCache() {
        logger.debug("getAllRulesWithCache");

        return getAllRulesFromDb();
    }

    /**
     * 从数据库中获取全部
     *
     * @return java.util.List<qc.module.qms.entity.QmsElement>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    public List<QmsAlarmDataRule> getAllRulesFromDb() {
        logger.debug("getAllRulesFromDb 从数据库中查询所有点号数据告警规则信息");
        LambdaQueryWrapper<QmsAlarmDataRule> wrapper = new LambdaQueryWrapper<>();

        //排序，按规则名称
        wrapper.orderByAsc(QmsAlarmDataRule::getRulename);

        //查询数据
        List<QmsAlarmDataRule> ens = ruleRepository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0)
            return ens;

        return null;
    }

    /**
     * 更新缓存，不管是否有缓存数据都进行更新
     *
     * @return java.util.List<qc.module.qms.entity.QmsAlarmDataPoint>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @CachePut(value = Cache_Key_Rule_Point)//更新缓存，不管是否有缓存数据都进行更新
    public List<QmsAlarmDataPoint> rulePointCachePut() {
        logger.debug("rulePointCachePut");

        return getAllRulePointRelationsFromDb();
    }

    /**
     * 清除缓存
     *
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @CacheEvict(value = Cache_Key_Rule_Point)//清除缓存
    public void rulePointCacheEvict() {
        logger.debug("rulePointCacheEvict");
    }

    /**
     * 获取全部；有缓存时从缓存获取，没有缓存时从数据库中获取并放入缓存
     *
     * @return java.util.List<qc.module.qms.entity.QmsAlarmDataPoint>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    @Cacheable(value = Cache_Key_Rule_Point)
    public List<QmsAlarmDataPoint> getAllRulePointRelationsWithCache() {
        logger.debug("getAllRulePointRelationsWithCache");

        return getAllRulePointRelationsFromDb();
    }

    /**
     * 从数据库中获取全部
     *
     * @return java.util.List<qc.module.qms.entity.QmsAlarmDataPoint>
     * @author QuCheng Tech
     * @since 2025/10/22
     */
    public List<QmsAlarmDataPoint> getAllRulePointRelationsFromDb() {
        logger.debug("getAllRulePointRelationsFromDb 从数据库中查询所有点号数据告警规则与点号关联信息");
        LambdaQueryWrapper<QmsAlarmDataPoint> wrapper = new LambdaQueryWrapper<>();

        //排序，按规则ID、点号ID、时段类型、数据值字段
        wrapper.orderByAsc(QmsAlarmDataPoint::getRuleid);
        wrapper.orderByAsc(QmsAlarmDataPoint::getPointid);
        wrapper.orderByAsc(QmsAlarmDataPoint::getPointtmseg);
        wrapper.orderByAsc(QmsAlarmDataPoint::getValuefield);

        //查询数据
        List<QmsAlarmDataPoint> ens = pointRepository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0)
            return ens;

        return null;
    }
}
